$(function () {
    //获取url
    var url = localStorage.getItem("ajaxUrl");
    //console.log(url);
    var insert = JSON.parse(localStorage.getItem("arr_role"))["silk"]["insert"];
    var apiUrl = "scheduleplan";

    $(".icon-left").on("click", function () {
        window.location.href = "silk.html";
    });

    $(".head h1").text(localStorage.getItem("silkMachine") + "计划列表");

    //点击 刷新 按钮  重新加载页面
    $(".page .head").on("click", "span.icon-refresh-1-copy", function () {
        window.location.href = "silk_ckjh.html";
    });

    //获取本机台的作业计划
    $.ajax({
        headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
        type: "post",
        url: url + '/app/silk/getControlsDetailsByMachine',
        data: {
            machine: localStorage.getItem("silkMachine")
        },
        dataType: 'json',
        json: 'callback',
        success: function (res) {
            if (res.success) {
                var data = res.result;
                var allWeight = 0;
                if (!data || data.length == 0) {
                    $alert("暂无作业计划！");
                } else {
                    $("#ul_data").empty();
                    var data_html = '';
                    for (var i = 0; i < data.length; i++) {
                        if (data[i].shopCode == "F03") {
                            apiUrl = "scheduleplanThird";
                        }
                        var inventoryMaterials = [];
                        if (data[i].inventoryMaterials && data[i].inventoryMaterials.length > 0) {
                            for (var j = 0; j < data[i].inventoryMaterials.length; j++) {
                                inventoryMaterials.push(data[i].inventoryMaterials[j].inventoryMaterialNameSpecification);
                            }
                        }
                        var wirerodfactoryCode = [];
                        if (data[i].wirerodmanufacturers && data[i].wirerodmanufacturers.length > 0) {
                            for (var j = 0; j < data[i].wirerodmanufacturers.length; j++) {
                                wirerodfactoryCode.push(data[i].wirerodmanufacturers[j].wirerodmanufacturerCode);
                            }
                        }
                        var f03html = "";
                        if (data[i].shopCode == "F03") {
                            f03html = '<p><span>单轴米长：</span><span class="axleLength sp">' + (data[i].axleLength ? data[i].axleLength : "0") + '</span>' +
                                '<span>轴数：</span><span class="axleAmount sp">' + (data[i].axleAmount ? data[i].axleAmount : "0") + '</span>' +
                                '<span>完成轴数：</span><span class="axleAmount sp">' + (data[i].completeWheelCount ? data[i].completeWheelCount : "0") + '</span></p>';
                        } else {
                            f03html = '';
                        }
                        data_html += '<li><div class="row no-gutter"><div class="col-100">' +
                            '<p class="p_none"><span>id</span><span class="id sp">' + data[i]._id + '</span></p>' +
                            '<p><span>排产单：</span><span class="workplanNumber sp">' + (data[i].workplanNumber ? data[i].workplanNumber : "") + '</span></p>' +
                            '<p><span>订单类型：</span><span class="saleordertype sp">' + (data[i].saleordertype ? data[i].saleordertype : "") + '</span></p>' +
                            '<p><span>机台：</span><span class="machine sp">' + (data[i].machine ? data[i].machine : "") + '</span></p>' +
                            '<p><span>工序名称：</span><span class="inventoryName sp">' + (data[i].inventoryName ? data[i].inventoryName : "") + '</span><span>规格：</span>' +
                            '<span class="inventorySpecification sp">' + data[i].inventorySpecification + '</span></p>' +
                            '<p><span>计划量：</span><span class="planQuantity sp">' + data[i].planQuantity + '</span>' +
                            '<span>完成量：</span><span class="completeQuantity sp">' + (data[i].completeQuantity ? Number(parseFloat(data[i].completeQuantity).toFixed(3)) : 0) + '</span></p>' +
                            f03html +
                            '<p><span>开始时间：</span><span class="in_time sp">' + (data[i].planStartTime ? data[i].planStartTime : "") + '</span></p>' +
                            '<p><span>结束时间：</span><span class="in_time sp">' + (data[i].planCompleteTime ? data[i].planCompleteTime : "") + '</span></p>' +
                            '<p><span>直径允差-：</span><span class="diameterToleranceMin sp">' + (data[i].diameterToleranceMin || "") + '</span>' +
                            '<span>直径允差+：</span><span class="diameterToleranceMax sp">' + (data[i].diameterToleranceMax || "") + '</span></p>' +
                            '<p><span>客户：</span><span class="customerName sp">' + (data[i].customerName) + '</span></p>';
                        if (inventoryMaterials.length > 0) {
                            data_html += '<p><span>原料：</span><span class="inventoryMaterials sp">' + inventoryMaterials + '</span></p>';
                        }
                        if (wirerodfactoryCode.length > 0) {
                            data_html += '<p><span>钢厂：</span><span class="wirerodfactoryCode sp">' + wirerodfactoryCode + '</span></p>';
                        }
                        if (data[i].comment) {
                            data_html += '<p><span>备注：</span><span class="comment sp">' + (data[i].comment ? data[i].comment : "") + '</span></p>';
                        }
                        if (data[i].plan && data[i].plan.contractcomment) {
                            data_html += '<p><span>订单备注：</span><span class="saleorderComment sp">' + (data[i].plan.saleorderComment ? data[i].plan.contractcomment : "") + '</span></p>';
                        }
                        if (data[i].plan && data[i].plan.workplancomment) {
                            data_html += '<p><span>排产备注：</span><span class="workplanComment sp">' + (data[i].plan.workplanComment ? data[i].plan.workplanComment : "") + '</span></p>';
                        }
                        data_html += '</div></li>';
                    }
                    $("#ul_data").append(data_html);
                    //$("#ul_data .li_plan").hide();
                }
                //改变 总计数
                $(".content div.ul_foot span.sp_js").html($("#ul_data li").length);
            } else {
                console.log(res);
                $alert(res.message);
            }
        },
        error: function (error) {
            console.log(error);
            $alert(error.responseText);
        }
    });

    //点击 计划 是否 查单
    $("#ul_data").on("click", 'li', function () {
        var len = $(".content div.ul_foot span.sp_js").text();
        var _id = $(this).find("span.id").text();
        var workplanNumber = $(this).find("span.workplanNumber").text();
        if (Number(len) > 1 && insert) {
            $.confirm("确认将" + workplanNumber + "插单生产吗？", function () {
                loading_show("插单中...");
                $.ajax({
                    headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
                    type: "post",
                    url: url + '/api/' + apiUrl + '/wire/insert',
                    data: {
                        _id: _id
                    },
                    dataType: 'json',
                    json: 'callback',
                    success: function (res) {
                        if (res.success) {
                            loading_hide();
                            $alert("插单成功");
                            setTimeout(function () {
                                window.location.href = "silk_ckjh.html";
                            }, 1400)
                        } else {
                            loading_hide();
                            $alert(res.message);
                        }
                    },
                    error: function (error) {
                        console.log(error);
                        loading_hide();
                        $alert(error.responseText);
                    }
                });
            })
        }
        // if ($(this).find(".li_plan").css("display") == "none") {
        //     $(this).find(".li_plan").show();
        // } else {
        //     $(this).find(".li_plan").hide();
        // }
    });

});
